package wiki.zimo.certificate_manage.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import wiki.zimo.certificate_manage.constant.Constant;
import wiki.zimo.certificate_manage.dao.LeaderDao;
import wiki.zimo.certificate_manage.dao.StudentDao;
import wiki.zimo.certificate_manage.dao.TeacherDao;
import wiki.zimo.certificate_manage.domain.Leader;
import wiki.zimo.certificate_manage.domain.Student;
import wiki.zimo.certificate_manage.domain.Teacher;
import wiki.zimo.certificate_manage.domain.User;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 *
 * @author 子墨
 * @date 2018/10/12 16:04
 * @describe 处理用户登录
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private StudentDao studentDao;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private LeaderDao leaderDao;

    /**
     * 处理访问登录页面的请求
     * @author 子墨
     * @date 2018/10/12 16:05
     * @params [map]
     * @return java.lang.String
     */
    @RequestMapping("")
    public String login(){
        return "login";
    }

    /**
     * @auther: 子墨
     * @date: 2018/10/28 19:58
     * @describe: 修改密码页
     * @param: []
     * @return: java.lang.String
     * @version v1.0
     */
    @RequestMapping("/password")
    public String password(){
        return "password";
    }

    /**
     * 用户登录接口
     * @author 子墨
     * @date 2018/10/17 19:19
     * @params [type, account, password]
     * @return java.lang.String
     */
    @RequestMapping("/userLogin")
    public String userLogin(Map<String,Object> map,
                            HttpSession session,
                            @RequestParam(value = "type",required = true) String type,
                            @RequestParam(value = "account",required = true) String account,
                            @RequestParam(value = "password",required = true) String password){
        User user = new User();
        String errMsg = "登录失败，账户或密码错误";
        if (type.equalsIgnoreCase(Constant.ROLE_STUDENT)){
            Student student = studentDao.findStudentByStuNumAndStuPassword(account,password);
            if (student != null){
                user.setStudent(student);
                user.setRole(Constant.ROLE_STUDENT);
                session.setAttribute(Constant.USER,user);
                return "redirect:/index";
            }else {
                map.put(Constant.MSG,errMsg);
                return login();
            }
        }else if(type.equalsIgnoreCase(Constant.ROLE_TEACHER)){
            Teacher teacher = teacherDao.findTeacherByTeacherNumAndTeacherPassword(account,password);
            if (teacher != null){
                user.setTeacher(teacher);
                user.setRole(Constant.ROLE_TEACHER);
                session.setAttribute(Constant.USER,user);
                return "redirect:/index";
            }else {
                map.put(Constant.MSG,errMsg);
                return login();
            }
        }else if(type.equalsIgnoreCase(Constant.ROLE_LEADER)){
            Leader leader = leaderDao.findLeaderByTeacherNumAndTeacherPassword(account,password);
            if (leader != null){
                user.setLeader(leader);
                user.setRole(Constant.ROLE_LEADER);
                session.setAttribute(Constant.USER,user);
                return "redirect:/index";
            }else {
                map.put(Constant.MSG,errMsg);
                return login();
            }
        }
        return login();
    }

    /**
     * 用户退出登录
     * @author 子墨
     * @date 2018/10/17 20:04
     * @params [session]
     * @return java.lang.String
     */
    @RequestMapping(value = "/userLogout")
    public String userLogout(HttpSession session){
        session.removeAttribute(Constant.USER);
        return login();
    }

    /**
     * @auther: 子墨
     * @date: 2018/10/28 19:50
     * @describe: 修改登录密码接口
     * @param: [password]
     * @return: java.lang.String
     * @version v1.0
     */
    @RequestMapping(value = "/changLoginPassword")
    @ResponseBody
    public String changLoginPassword(HttpSession session,
                                     @RequestParam(value = "password",required = true)String password){
        User user = (User) session.getAttribute(Constant.USER);
        if (user == null){
            return "error";
        }
        String role = user.getRole();
        switch (role){
            case Constant.ROLE_LEADER:
                Leader leader = user.getLeader();
                leader.setTeacherPassword(password);
                leaderDao.save(leader);
                break;
            case Constant.ROLE_TEACHER:
                Teacher teacher = user.getTeacher();
                teacher.setTeacherPassword(password);
                teacherDao.save(teacher);
                break;
            case Constant.ROLE_STUDENT:
                Student student = user.getStudent();
                student.setStuPassword(password);
                studentDao.save(student);
                break;
        }
        return "success";
    }
}
